package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.manager.redis.RedisManager;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.common.constant.RedisKeyConsts;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.graborder.GrabOrderWorkDTO;
import com.zmn.oms.model.dto.graborder.MqGrabOrderDTO;
import com.zmn.oms.model.dto.work.modify.must.OrderWorkGrabDTO;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.grab.NonGridGrabOrderBService;
import com.zmn.oms.zmn.business.interfaces.work.OrderWorkGrabBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 抢单队列
 */
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
@Slf4j
public class OrderGrabListener {

    @Autowired
    private OrderWorkGrabBService orderWorkGrabBService;
    @Autowired
    private OrderWorkService orderWorkService;
    @Resource
    private RedisManager redisManager;
    @Autowired
    private NonGridGrabOrderBService nonGridGrabOrderBService;

    /**
     * 抢单时间结束
     *
     * @param message
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.GRAB_ORDER_END_TIME_MQ_TAG)
    public void grabOrderEnd(String message) {
        log.info("#oms#grab#delete# 抢单消费【{}】", message);
        if (StringUtil.isBlank(message)) {
            return;
        }

        try {
            MqGrabOrderDTO dto = JSON.parseObject(message, MqGrabOrderDTO.class);

            // 获取存根
            String key = String.format(RedisKeyConsts.GRAB_CONSUME_ORDER_KEY, dto.getWorkId());
            String value = redisManager.get(key);

            if (StringUtil.isBlank(value)) {
                return;
            }

            // 时间对不上，不消费
            if (dto.getDateTime() != Long.parseLong(value)) {
                return;
            }

            OrderWork orderWork = orderWorkService.findOrderWorkByKeySrcMaster(dto.getWorkId(), dto.getWorkId());
            // 已派单不消费
            if (orderWork.getStatus() >= OrderStatusConsts.WORK_STATUS_DISTRIBUTE) {
                return;
            }
            OrderWorkGrabDTO orderWorkGrabDTO = new OrderWorkGrabDTO();
            orderWorkGrabDTO.setOrderId(dto.getWorkId());
            orderWorkGrabDTO.setWorkId(dto.getWorkId());
            orderWorkGrabDTO.setOperator("系统");
            orderWorkGrabDTO.setOperatorId(1L);
            orderWorkGrabDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            orderWorkGrabDTO.setGrabType(GlobalConsts.NO);
            orderWorkGrabBService.saveGrabType(orderWorkGrabDTO);
        } catch (OmsBaseException e) {
            e.printStackTrace();
            log.error("#oms#grab#delete#：【{}】", e.getMessage());
        }
    }

    /**
     * 抢单时间结束
     *
     * @param message
     */
    @AliyunMQHandler(tags = OmsMqTagConsts.GRAB_NON_GRID_ORDER_END_TIME_MQ_TAG)
    public void grabNonGridOrderEnd(String message) {
        log.info("#oms#non_grid_grab#delete# 抢单消费【{}】", message);
        if (StringUtil.isBlank(message)) {
            return;
        }

        try {
            MqGrabOrderDTO dto = JSON.parseObject(message, MqGrabOrderDTO.class);

            // 获取存根
            String key = String.format(RedisKeyConsts.GRAB_CONSUME_NON_GRID_ORDER_KEY, dto.getWorkId());
            String value = redisManager.get(key);

            if (StringUtil.isBlank(value)) {
                return;
            }

            // 时间对不上，不消费
            if (dto.getDateTime() != Long.parseLong(value)) {
                return;
            }

            OrderWork orderWork = orderWorkService.findOrderWorkByKey(dto.getWorkId(), dto.getWorkId());
            // 已派单不消费
            if (orderWork.getStatus() >= OrderStatusConsts.WORK_STATUS_DISTRIBUTE) {
                return;
            }
            OrderWorkGrabDTO orderWorkGrabDTO = new OrderWorkGrabDTO();
            orderWorkGrabDTO.setOrderId(dto.getWorkId());
            orderWorkGrabDTO.setWorkId(dto.getWorkId());
            orderWorkGrabDTO.setOperator("系统");
            orderWorkGrabDTO.setOperatorId(1L);
            orderWorkGrabDTO.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_SYSTEM);
            orderWorkGrabDTO.setGrabType(GlobalConsts.NO);
            orderWorkGrabDTO.setEndNonGridGrab(GlobalConsts.YES);
            orderWorkGrabDTO.setGrabWay(GlobalConsts.YES);
            nonGridGrabOrderBService.modifyGrabType(orderWorkGrabDTO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("#oms#non_grid_grab#delete#：【{}】", e.getMessage());
        }
    }

    @AliyunMQHandler(tags = OmsMqTagConsts.GRAB_ORDER_DISTRIBUTE_TAG)
    public void distribute(String message) {
        log.info("#oms#grab#distribute# 抢单消费【{}】", message);
        if (StringUtil.isBlank(message)) {
            return;
        }

        try {
            GrabOrderWorkDTO grabOrderWorkDTO = JSON.parseObject(message, GrabOrderWorkDTO.class);
            orderWorkGrabBService.grabDistributeOrder(grabOrderWorkDTO);
        } catch (OmsBaseException e) {
            e.printStackTrace();
            log.error("#oms#grab#distribute#：【{}】", e.getMessage());
        }
    }


}
